Okay.
So today we are going to start off looking at loops.
So there are three types of loops we're going to look at today.
We're going to look at the first type which are called while loops okay.
So add a let's add a part into our game.
And let's also add a script inside of this part here okay I'm going to move this part over here a little
bit okay.
So again let's create a variable for the part.
Because the script is inside of the part.
It'd be easier to say just script parent.
Instead of saying game dot workspace dot pi.
It would just be quicker to use.
Okay so let's talk about a while loop.
So a while loop um they're similar ish to if statements we looked at last lesson.
Okay.
Um.
In which weather condition.
That it checks.
However, the difference with a while loop is if the condition.
Um, yeah.
The difference with a while loop is the code that you write.
Um, it continuously runs the code, so it runs it again and again and again until your condition is
no longer met.
So with an if statement, it first checks your condition.
If it's met, it runs your code.
Your while loop runs your code until the statement is not is not met.
Okay.
Um, so while something is happening basically, um, so let's give an let's write this down.
So, um, just like how with if if variables.
Sorry if variables.
Um, just like how with if statements.
Um, you would use the if keyword.
We're going to use the while keyword for a while loop okay.
And then you write down your condition.
So this is what you wanted to check in order to keep on repeating the code.
So let's say um part dot anchored okay.
So while the part is anchored now remember we don't need to say is equal to equal to true.
We discussed this in our if loops lesson because, um, the condition needs to have the value have a
have a value of true in order to work.
Okay.
So this here needs to be true, which is also the same as this being true because you're saying if the
anchored property has the value of true.
Okay.
We did discuss that a bit earlier on though.
Um, so yeah, press enter after that.
And you can see it fills in some blanks for us as well.
It puts do at the end of that line and then end at the bottom of the line.
You don't need to remember that as long as you just press enter it will automatically put those in for
you.
Okay.
So while loops continuously run code.
So for example anything you write in here, it will basically it will keep on running that code until
the part is no longer anchored.
So while the part is anchored it will be running our code, which is great.
But there's a couple of things we need to talk about with that.
So I mean, first it's while if, if, if if statements when you're using those, they're best just
to change one property at a time.
So if this happens change the property to this value.
But with while with while loops because it's continuously doing it, it wouldn't make too much sense
to say something like part dot color is equal to color three dot new.
Uh, 111 for example, because you're kind of you're continuously changing the color to white even when
it already is.
So the best case to use this is when you're actually changing the property value.
So you're adding a value onto it.
You're taking away a value.
That kind of stuff is where it's most useful, which is where we're going to be using it today.
Okay.
Um, and the second issue I'm going to just show you here.
So let's say when it's anchored, I want to just use a print function and have it print part is anchored.
So while the part is anchored it's going to print part is anchored.
Okay.
Um, and I'm going to make sure it's anchored, which it is here.
Okay.
So I'm going to run the game and I'm going to explain what's wrong with that while it's running.
Okay.
Um, the issue with that okay, is that it's going to it's, it's, it's printing, um, part is anchored
while the part is anchored.
Okay.
Makes sense, but, um, it's doing it as fast as it can.
Basically.
We haven't putting any we haven't put any kind of time limit in there.
So it's it's just going to be printing it as fast as it fast as it can.
Um, so what's going to happen is it's going to kind of overload our game because it's trying to print,
um, prints.
That's the part is anchored like, um, hundreds of thousands of times each second.
So it's completely overloading our game.
And it sometimes takes a while, but it would hopefully show up here in a little bit in the output.
What the kind of, um, the outcome of that is.
Um, but that's why it's very important to use the wait function when you are looking at while loops.
Okay, a while loop must always contain a wait function, even if it's a very small amount, it just
prevents it from overloading the, um, your server with all of these different things to do.
Because if one script is trying to trying to, um, print some.
Like hundreds of thousands of times a second.
It's not going to be able to do anything else.
And we can see here it's finished.
Its printed part is anchored 4996 times, which could vary depending on what you're using this on.
And then you get an error here saying script timeout exhausted allowed execution time, which basically
means the script took too long to run basically.
So it was sending too many requests.
It was trying to get it to do too many things.
Okay.
Um, so that's just an example about what happens if you don't include a wait.
Okay.
So even if it's 0.001 seconds, that's still completely okay.
You just need some kind of wait with some number written inside the brackets, and then it will work
fine.
Because if you did this, for example, it's now printing it ten times a second, which is quite a lot
of times of course.
But the game can very easily handle handle that.
Okay.
Um, so very very important when you are looking at while loops, make sure you include a wait.
Okay.
Now I've got those out of the way.
Let's have a look at a proper use of this.
So let's say I want this part to to move around okay?
I want it to kind of move upwards.
Maybe we're kind of maybe coding a rocket, a rocket ship going upwards.
Obviously the actual rocket ships are coded much.
Um, the code for actual rocket ships in games were much more complicated, but let's just make a simple
one.
Okay.
Um, so imagine this, our rocket ship, and we're going to try and code it so it goes upwards.
Okay.
So we're changing the position of this part.
So parts dot position is the name of the property we're wanting to change is equal to.
Now remember we're trying to add we're going to be adding a value onto the position.
Because if we just set one value for the position it would just be giving it the same position ten times
a second, which wouldn't be helpful at all.
So instead, um, we add a value onto the position.
So it's always increasing from the last position, which is how we make it move.
Okay.
Um, so let's say part dot position is equal to we say is equal to part dot position.
So the position is equal to the current position plus a value of Vector3 value.
And if we want to do it along the y axis y axis goes vertically okay.
So we'd be saying zero comma one comma zero.
And it'll be increasing by one starred every 0.1 seconds.
As long as it's anchored.
If it becomes anchored it will stop.
So let's try this.
Let's see what happens.
We can see it's increasing up there and how it's going up.
Like that with hope.
It's quite cool because you can see it happening in real time as well.
Um, now, obviously it's, um, it's quite jumpy.
It goes up and jumps.
So if you wanted to fix that instead, we could, um, say to move it 0.1 studs every 0.01 seconds,
for example.
And then in that case it would move the same speed but just much smoother.
As you can see there, it's much nicer to look at.
Okay.
Um, so just make sure that you're not you're not doing it too many times as if you have lots of different
while loops but are doing that are that's changing a property a thousand times per second.
It's going to make your game very laggy.
And some, some, some players might not even be able to join it because it's that laggy.
It's that difficult to run on their computer.
Okay, so just be careful with this.
I wouldn't go anything lower than 0.01 seconds.
Probably some things you might need to, but I would try to at least stay above that.
Um, so you're not overloading your game.
Okay.
Um, but yeah.
So that's your while loop.
Okay.
So your condition goes here and it runs whatever code is inside there as fast as it can.
While that condition is true, and just like how with your if statements as well, you can also put
ends in here as well.
So if we wanted it, we wanted it to work.
When the part is anchored and you can see it, we could say also parts and bouncy does not equal one.
Okay.
So you can also put your ends ORS knots or that kind of stuff.
All the same stuff we learnt with our if statements.
You can go inside here, but we just do it in a slightly different way with it doing it as fast as it
can.
Okay, so there, really cool.
A really cool loop, um, to learn.
So um, hopefully you like those.
Um, have a try out at VAT and join me in the next lesson where we will be looking at for loops, the
second type of loop we're going to look at.